package com.bysj.lms.service.impl;

import com.bysj.lms.common.Const;
import com.bysj.lms.mapper.StatisticsMapper;
import com.bysj.lms.pojo.dto.CourseDto;
import com.bysj.lms.pojo.vo.*;
import com.bysj.lms.service.*;
import com.bysj.lms.utils.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class StatisticsServiceImpl implements IStatisticsService {

    @Autowired
    ICourseService courseService;
    @Autowired
    StatisticsMapper statisticsMapper;
    @Autowired
    ILmsFaService lmsFaService;
    @Autowired
    ILmsLveService lmsLveService;
    @Autowired
    ILmsEmService lmsEmService;
    @Override
    public TSSResult tcCompletion(String term, String tname) {
        TSSResult tssResult = new TSSResult();
        int finishTNumber = 0;
        int unfinishTNumber = 0;
        int finishCNumber = 0;
        int unfinishCNumber = 0;
        int totalCNumber = 0;
        List<TCStatistics> cs = statisticsMapper.getCS(term, tname);
        for(TCStatistics tcStatistics:cs){
            List<CourseStatistics> courseList = tcStatistics.getCourseList();
            totalCNumber = totalCNumber+courseList.size();
            for(CourseStatistics courseStatistics:courseList){
                double completePeriod=0;
                if(StringUtil.isNotEB(courseStatistics.getSection())){
                    completePeriod =courseStatistics.getSection().split(",").length * 0.75;
                }
                courseStatistics.setCompletePeriod(completePeriod);
                Integer planPeriod = courseStatistics.getPlanPeriod();
                double rate = completePeriod / planPeriod >= 1 ? 1 : completePeriod / planPeriod;
                courseStatistics.setFinishRate(rate);
                if(rate<1){unfinishCNumber=unfinishCNumber+1;}
                else {finishCNumber = finishCNumber+1;}
            };
            int flag=0;
            for (CourseStatistics statistics:courseList) {
                if(statistics.getFinishRate()<1){
                    flag=1;
                }
            }
            if(flag==0) finishTNumber++;
            else unfinishTNumber++;
        };
        tssResult.setFinishCNumber(finishCNumber);
        tssResult.setUnfinishCNumber(unfinishCNumber);
        tssResult.setTotalCNumber(totalCNumber);
        tssResult.setFinishTNumber(finishTNumber);
        tssResult.setUnfinishTNumber(unfinishTNumber);
        tssResult.setList(cs);
        tssResult.setTotalTNumber(cs.size());
        return tssResult;
    }

    @Override
    public BRStatistics brStatistics(LocalDate startDate, LocalDate endDate) {
        BRStatistics brs = statisticsMapper.getBRS(startDate, endDate);
        long count = lmsFaService.count();
        long count1 = lmsLveService.count();
        brs.setBrRate((double)brs.getBrTotal()/(count+count1));
        return brs;
    }

    @Override
    public List<TBRStatistic> tbrStatistics(String uid, String uname, LocalDate startDate, LocalDate endDate) {
        List<TBRStatistic> tbrs = statisticsMapper.getTBRS(uid, uname, startDate, endDate);
        long count = lmsFaService.count();
        long count1 = lmsLveService.count();
        tbrs.forEach(tbrStatistic -> {
            Integer brTotal = tbrStatistic.getBrStatistics().getBrTotal();
            System.out.println(count+" "+count1+" "+brTotal);
            tbrStatistic.getBrStatistics().setBrRate((double)brTotal /(count+count1));
        });
        return tbrs;
    }

    @Override
    public List<SBBRStatistics> sbbrStatistics(LocalDate startDate, LocalDate endDate) {
        List<SBBRStatistics> sbbrStatistics = statisticsMapper.sbbrStatistics(startDate, endDate);
        long count = lmsFaService.count();
        long count1 = lmsLveService.count();
        sbbrStatistics.forEach(sbbrStatistics1 -> {
            if(sbbrStatistics1.getType().equals("固定资产")){
                sbbrStatistics1.setBRate((double)sbbrStatistics1.getBTime()/count);
            }else{
                sbbrStatistics1.setBRate((double)sbbrStatistics1.getBTime()/count1);
            }
            if(sbbrStatistics1.getRTime()!=0){
                sbbrStatistics1.setRRate((double)sbbrStatistics1.getBTime()/sbbrStatistics1.getRTime());
            }
        });
        return sbbrStatistics;
    }

    @Override
    public PageInfo sbUseStatistics(Integer page,Integer count,String name, String term) {

        PageHelper.startPage(page,count);
        String[] s = term.split(" ");
        String[] years = s[0].split("-");
        int year = Integer.parseInt(years[0]);
        String substring = s[1].substring(0,1);
        System.out.println(substring);
        int t = Integer.parseInt(substring);
        List<SBUseStatistics> sbUseStatistics = statisticsMapper.sbUseStatistics(name,year, t);
        sbUseStatistics.forEach(sbu->{
            Integer workTime = sbu.getWorkTime();
            sbu.setUseRate((double)workTime/Const.workTime);
        });
        return new PageInfo(sbUseStatistics);
    }

    @Override
    public WXStatistics wxStatistics(String startDate, String endDate) {
        WXStatistics wxStatistics = statisticsMapper.wxStatistics(startDate, endDate);
        return wxStatistics;
    }

    @Override
    public PageInfo sbwxStatistics(Integer page, Integer count, String startDate, String endDate) {
        PageHelper.startPage(page,count);
        List<SBWXStatistics> sbwxStatistics = statisticsMapper.sbwxStatistics(startDate, endDate);
        long num = lmsEmService.count();
        sbwxStatistics.forEach(sb->{sb.setWxRate((double) sb.getTime()/num);});
        return new PageInfo(sbwxStatistics);
    }
}
